package com.weipai.demo.controller;

import com.weipai.demo.enums.RetCode;
import com.weipai.demo.model.Picture;
import com.weipai.demo.model.PictureCategory;
import com.weipai.demo.service.FileUploadService;
import com.weipai.demo.service.PictureService;
import com.weipai.demo.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 赵项阳
 * @Date: 2019/6/14 14:23
 * @Version 1.0
 */

@RestController
public class PictureController {

    @Autowired
    private PictureService pictureService;
    @Autowired
    private FileUploadService fileUploadService;


    //展示轮播图
    @RequestMapping(value = "/picture/show")
    public Response show(Picture picture) {
        return pictureService.selectByShowPaging(picture);
    }
    //轮播图类别显示
    @RequestMapping(value = "/picture/classShow")
    public Response classShow(PictureCategory pictureCategory) {
        return pictureService.selectByShowClass(pictureCategory);

    }

    //==================================================================================================================

    //删除轮播图
    @RequestMapping(value = "/picture/delePicture")
    public Response delePicture(Picture picture) {
        return pictureService.delePicture(picture);
    }


    //添加轮播图
    @RequestMapping(value = "/picture/uploadingImg", method = RequestMethod.POST)
    public Response uploadingImg(HttpServletRequest request, Picture picture, MultipartFile img) {
        Response response = new Response();
        if (img != null) {
            String result = fileUploadService.saveImgUpload(request, img);
            if (result.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
            } else if (result.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if (result.equals("error")) {
                response.setCode(RetCode.IMG_ERROR);
            } else {
                picture.setPictureUr(result);
                response = pictureService.savePicture(picture);
            }
        }else {
            picture.setPictureUr(picture.getPictureUr());
            response = pictureService.savePicture(picture);
        }
        return response;

    }

    //==================================================================================================================

    //编辑轮播图
    @RequestMapping(value = "/picture/editor")
    public Response editor (Picture picture){
        return pictureService.editor(picture);
    }

    //==================================================================================================================

    //首页添加广告图片
    @RequestMapping(value = "/picture/advertising", method = RequestMethod.POST)
    public Response advertising(HttpServletRequest request, Picture picture, MultipartFile img) {
        Response response = new Response();
        if (img != null) {
            String result = fileUploadService.saveImgUpload(request, img);
            if (result.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
            } else if (result.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if (result.equals("error")) {
                response.setCode(RetCode.IMG_ERROR);
            } else {
                picture.setPictureUr(result);
                response = pictureService.savePicture(picture);
            }
        }else {
            picture.setPictureUr(picture.getPictureUr());
            response = pictureService.savePicture(picture);
        }
        return response;
    }
    //==================================================================================================================

    //APP展示广告图
    @RequestMapping(value = "/picture/AdvertisingShow")
    public Response advertisingShow(Picture picture) {
        return pictureService.advertisingShow(picture);
    }


}
